﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.Migrations;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace Hr.DAL
{
    public partial class BaseDAL<T> where T:class,new()
    {
        // 创建EF对象
        private DbContext db = DbContextFactory.Create();

        #region 1.0 添加
        public void Add(T t)
        {
            db.Set<T>().Add(t);
        }
        #endregion


        #region 2.0 删除
        public void Delete(T t)
        {
            db.Set<T>().Remove(t);
        }
        #endregion

        #region 3.0 更新
        public void Update(T t)
        {
            db.Set<T>().AddOrUpdate(t);
        }
        #endregion

        #region 4.0 查询
        public IQueryable<T> GetModels(Expression<Func<T,bool>> whereLambda)
        {
            return db.Set<T>().Where(whereLambda);
        }
        #endregion

        #region 5.0 分页查询
        public IQueryable<T> GetModelsByPage<type>(int page,int limit,bool isAsc,Expression<Func<T,type>> OrderByLambda,Expression<Func<T,bool>> whereLambda)
        {
            //是否升序
            if (isAsc)
            {
                return db.Set<T>().Where(whereLambda).OrderBy(OrderByLambda).Skip((page - 1) * limit).Take(limit);
            }
            else
            {
                return db.Set<T>().Where(whereLambda).OrderByDescending(OrderByLambda).Skip((page - 1) * limit).Take(limit);
            }
        }
        #endregion

        #region 6.0 提交保存
        public bool Savechanges()
        {
            return db.SaveChanges() > 0;
        }
        #endregion
    }
}
